const fs = require("fs");
const path = require("path");
const excelToJson = require("convert-excel-to-json");
const xlsx = require("node-xlsx");

// excel文件类径
const excelFilePath = "./excel.xlsx";
// 写一个文件
const writeFile = (path, data) => {
  return new Promise((resolve) => {
    fs.writeFile(path, data, (err) => {
      if (err) {
        console.log("err", err);
        return;
      }
      console.log(`${path} 写入成功!`);
      resolve();
    });
  });
};
// 读取文件
const readFile = (url) => {
  return new Promise((resolve) => {
    fs.readFile(url, "utf-8", (err, data) => {
      if (err) {
        throw Error(err);
      }
      console.log(`读取${url}成功！`);
      resolve(data);
    });
  });
};
// 判断文件类型是js或者ts 文件
const judgeJSXFile = (path) => {
  if (!path) {
    return;
  }
  let fileType = path.split(".")[1];
  if (
    fileType === "tsx" ||
    fileType === "jsx" ||
    fileType === "ts" ||
    fileType === "js"
  ) {
    return true;
  }
  return;
};
//读取文件目录
const listFile = (dir, list = []) => {
  var arr = fs.readdirSync(dir);
  arr.forEach(function (item) {
    var fullpath = path.join(dir, item);
    var stats = fs.statSync(fullpath);
    if (stats.isDirectory()) {
      listFile(fullpath, list);
    } else {
      if (judgeJSXFile(fullpath)) {
        list.push(fullpath);
      }
    }
  });
  return list;
};

(async () => {
  let result = {};
  let defaultLanguageJson = {};
  let standardJSon = {};

  // const sheets = xlsx.parse(excelFilePath);

  result = await excelToJson({
    sourceFile: excelFilePath,
  });
  // console.log(Reflect.ownKeys(result));
  defaultLanguageJson = result.Sheet1;
  // 将defaultLanguageJson 处理成标准的json 格式 key: '中文'
  for (let i = 1; i < defaultLanguageJson.length; i++) {
    console.log(defaultLanguageJson[i]);
    let itemKey = defaultLanguageJson[i];
    standardJSon[itemKey.A] = itemKey.F;
  }
  await writeFile("./defaultLanguageJson.json", JSON.stringify(standardJSon));
})();
